package com.enhe.gck.fg;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.enhe.gck.CarbonBaseConstant;
import com.enhe.gck.CarbonEnumKeyValue;
import com.enhe.gck.CarbonRelationType;
import com.enhe.gck.MockFreight;
import com.enhe.gck.com.oracle.OracleController;
import com.enhe.gck.com.oracle.OracleEntity;
import com.enhe.gck.communal.*;
import com.enhe.gck.item.*;
import com.enhe.gck.pojo.*;
import com.enhe.gck.utils.PDFUtil;
import com.kuangkie.carbon.common.*;
import com.kuangkie.carbon.enun.SortDirection;
import com.kuangkie.carbon.fg.*;
import com.kuangkie.carbon.fg.ops.FGOpsComplexus;
import com.kuangkie.carbon.fg.ops.ProRecordOps;
import com.kuangkie.carbon.fg.ops.ProRecordOpsBuilder;
import com.kuangkie.carbon.panel.CarbonPanel;
import com.kuangkie.carbon.record.*;
import com.kuangkie.core.u.e.B;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Repository(value = "hwane16677366042330866425862")
public class OrderHWANE16677BNB implements FirstRoundImproveFuncGroup, SecondRoundImproveFuncGroup, FetchFuncGroup ,FuseCallBackFuncGroup,RemoveFuncGroup{
	@Resource
	private IcCardHWANE16640BNB icCardHWANE16640BNB;
	@Resource
	private AsyncImpl asyncImpl;
	@Resource
	private ConfigHWANE16627BNB configHWANE16627BNB;
	@Resource
	private OracleController oracleController;
	@Resource
	private FileTemplateHWANE18473BNB fileTemplateHWANE18473BNB;

	@Override
	public synchronized void secondImprove(FuncGroupContext context, ProRecord proRecord, FGRecordComplexus recordComplexus,
										   ImproveResultFactory improveResultFactory) {
		Order order=proRecord.getBean(Order.class);
		log.info("QAQ新增或修改业务开始:{}",JSON.toJSON(order));
		if(PublicMethod.verifyIsStopCustomer(proRecord,improveResultFactory,PublicConstant.OR)){
			return;
		}

		if(proRecord.isNew()||isGood(order)){
			if(proRecord.isNew()){
				OperationLogHWANE21797BNB.save(improveResultFactory,6,1,proRecord.getRecordCode());
				initialize(order,improveResultFactory);
			}

			if(!String.valueOf(CarbonEnumKeyValue.操作命令_装车触发_zccf).equals(order.getCommand())){
				if(String.valueOf(CarbonEnumKeyValue.操作命令_运输商添加配送车辆_ysstjpscl).equals(order.getCommand())){
					log.info("配送生成取货码");
					if(String.valueOf(CarbonEnumKeyValue.订单信息_收货类型_配送_ps).equals(order.getCollectType())){
						if(StrUtil.isBlank(order.getPickupCode())){
							order.setPickupCode(PublicMethod.getPickupCode());
							improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_8位取货码,order.getPickupCode());
						}
						improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_补充车号,true);
						improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_派车时间,LocalDateTime.now());
					}
				}

				if(proRecord.isNew() ||
						String.valueOf(CarbonEnumKeyValue.订单记录_状态_已创建_ycj).equals(order.getState())||
						String.valueOf(CarbonEnumKeyValue.订单记录_状态_待过一磅_dgyb).equals(order.getState())
				){
					log.info("QAQ验证散装是否超过设定范围 与 输入重量或袋数");
					if(orderTypeInspection(proRecord,order,improveResultFactory)){
						return;
					}
				}
			}

			setFinishTime(order,improveResultFactory);

			if(String.valueOf(CarbonEnumKeyValue.操作命令_发送记录_同步_fsjltb).equals(order.getCommand())){
				log.info("QAQ操作命令_发送记录_同步_fsjltb");
				FGRelationCorrelation relation=CarbonPanel.getRecordDiscover(CarbonBaseConstant.订单记录_ddjl).getRelationCorrelation(order.getRecordCode());
				boolean isOk=saveOrderNew(order,relation,false);
				if(!isOk){
					log.info("同步失败，请查看发送记录");
					PublicMethod.returnMessage(improveResultFactory,PublicReturn.SYSTEM_2);
					return;
				}else {
					improveResultFactory.getCurrentProRecordOpsBuilder()
							.putAttribute(OrderHWANE16677Item.基本属性组_是否同步,true);
				}
			}

			if(!String.valueOf(CarbonEnumKeyValue.操作命令_装车触发_zccf).equals(order.getCommand())){
				log.info("清空单袋重量");
				if(!String.valueOf(CarbonEnumKeyValue.订单记录_类型_袋装_dz).equals(order.getType())){
					improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_单袋重量,null);
				}

				log.info("计算重量");
				order.setWeight(String.valueOf(CarbonEnumKeyValue.订单记录_类型_袋装_dz).equals(order.getType())?
						PublicMethod.multiplyBigDecimal(order.getBags(),order.getSingleWeight(),PublicConstant.weightDecimals,null).doubleValue():
						order.getWeight());
				improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_重量,order.getWeight());

				boolean isRetail =String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsRetail());
				if(isRetail){
					log.info("验证散户");
					AggregateVO aggregateVO = CarbonPanel
							.getRecordQueryFactory(CarbonBaseConstant.客户管理_khgl).getProConJunctionFactory()
							.addEqual(CarbonBaseConstant.COLUMN_ABP0001, proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_散户_客户管理))
							.goBack().queryCount();
					if(aggregateVO==null||aggregateVO.getCount()<1){
						PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_4);
						return;
					}
				}

				log.info("验证下单重量");
				//新
				if( String.valueOf(CarbonEnumKeyValue.订单记录_状态_已创建_ycj).equals(order.getState()) ||
						String.valueOf(CarbonEnumKeyValue.订单记录_状态_待过一磅_dgyb).equals(order.getState()) ||
						String.valueOf(CarbonEnumKeyValue.订单记录_状态_待装卸货_dzxh).equals(order.getState()) ||
						String.valueOf(CarbonEnumKeyValue.订单记录_状态_待过二磅_dgeb).equals(order.getState())
				){
					if(isRetail){
						if(!verifyWeightHandling(proRecord,improveResultFactory,order.getWeight(),true)){
							return;
						}
					}else {
						if(!verifyWeightHandling(proRecord,improveResultFactory,order.getWeight(),false)){
							return;
						}
					}
				}

				if(String.valueOf(CarbonEnumKeyValue.操作命令_订单_手工补录_ddsgbl).equals(order.getCommand())){
					log.info("QAQ操作命令_订单_手工补录_ddsgbl");
					inputHand(proRecord,improveResultFactory,order);
					return;
				}

				log.info("验证实际重量");
				//新
				if(String.valueOf(PublicConstant.OrderStateOver).equals(order.getState())
						|| String.valueOf(CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk).equals(order.getState())){
					if(isRetail){
						if(!verifyWeightHandlingNoAll(proRecord,improveResultFactory,order.getActualWeight(),true)){
							return;
						}
					}else {
						if(!verifyWeightHandlingNoAll(proRecord,improveResultFactory,order.getActualWeight(),false)){
							return;
						}
					}
				}

				log.info("处理扣款");
				//新
				if(String.valueOf(PublicConstant.OrderStateOver).equals(order.getState())||String.valueOf(CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk).equals(order.getState())){
					if(isRetail){
						deductionAll(proRecord,improveResultFactory,BigDecimal.valueOf(order.getActualWeight()),true,true);
					}else {
						deductionAll(proRecord,improveResultFactory,BigDecimal.valueOf(order.getActualWeight()),false,true);
					}
				}

				boolean flow1=ICCar(proRecord,improveResultFactory);
				if(!flow1){
					return;
				}
				calculateShipping(proRecord,order,improveResultFactory);

			}
		}else {
			// 客户不允许取消订单
			if (!String.valueOf(CarbonEnumKeyValue.订单记录_状态_已创建_ycj).equals(order.getState())) {
				CarbonUser user = PublicMethod.getInfo(CarbonBaseConstant.系统用户_xtyh, context.getUserCode(), CarbonUser.class);
				if (user != null && StrUtil.isNotBlank(user.getType()) && user.getType().contains(String.valueOf(CarbonEnumKeyValue.客户管理_客户类型_经销商_jxs))) {
					PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_35);
					return;
				}
			}
			returnTriggered(proRecord);
			deductionAll(proRecord,improveResultFactory,null,false,false);
		}


	}

	/**
	 * 校验下单量 及是否配置范围内 非袋装袋数 5或10的整数倍
	 * @return
	 */
	private boolean orderTypeInspection(ProRecord proRecord,Order order,ImproveResultFactory improveResultFactory){
		Goods goods=CarbonPanel.getRecordDiscover(CarbonBaseConstant.产品管理_cpgl).getRecordBean(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理),Goods.class,1);
		if(String.valueOf(CarbonEnumKeyValue.订单记录_类型_袋装_dz).equals(order.getType())){
			if(order.getBags()==null){
				return PublicMethod.returnMessageTrue(improveResultFactory,PublicReturn.ORDER_17);
			}
			//新 验证袋数为5或10的整数倍
			boolean zs = order.getBags() % 5 == 0;
			if(!zs){
				return PublicMethod.returnMessageTrue(improveResultFactory,PublicReturn.ORDER_32);
			}

			log.info("QAQ袋装验证产品范围");
			if(String.valueOf(CarbonEnumKeyValue.订单记录_类型_袋装_dz).equals(goods.getType())){
				//防止数据修改
				if((goods.getMinimum()!=null&&PublicMethod.compareToBigDecimal(goods.getMinimum(),order.getBags())>0)||
						(goods.getMost()!=null&&PublicMethod.compareToBigDecimal(goods.getMost(),order.getBags())<0)
				){
					return PublicMethod.returnMessageTrue(improveResultFactory,PublicReturn.ORDER_13);
				}
			}
		}else {
			if(order.getWeight()==null){
				return PublicMethod.returnMessageTrue(improveResultFactory,PublicReturn.ORDER_16);
			}
			improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_袋装数,null);
			log.info("QAQ散装验证产品范围");
			if(!String.valueOf(CarbonEnumKeyValue.订单记录_类型_袋装_dz).equals(goods.getType())){
				//防止数据修改
				if(goods.getMinimum()!=null&&PublicMethod.compareToBigDecimal(goods.getMinimum(),order.getWeight())>0||
						goods.getMost()!=null&&PublicMethod.compareToBigDecimal(goods.getMost(),order.getWeight())<0){
					return PublicMethod.returnMessageTrue(improveResultFactory,PublicReturn.ORDER_13);
				}
			}
		}
		return false;
	}
	/**
	 * 设置完成时间
	 */
	private void setFinishTime(Order order,ImproveResultFactory improveResultFactory){
		ProRecordOpsBuilder setData=improveResultFactory.getCurrentProRecordOpsBuilder();
		log.info("设置完成时间");
		if(order.getFinishTime()==null){
			if(String.valueOf(CarbonEnumKeyValue.订单记录_状态_已出厂_ycc).equals(order.getState())){
				Order orderOrigin=CarbonPanel.getRecordDiscover(CarbonBaseConstant.订单记录_ddjl).getRecordBean(order.getRecordCode(),Order.class);
				if(orderOrigin!=null){
					if(!String.valueOf(CarbonEnumKeyValue.订单记录_状态_已出厂_ycc).equals(orderOrigin.getState())){
						log.info("未完成过，设置时间");
						setData.putAttribute(OrderHWANE16677Item.基本属性组_完成时间, LocalDateTime.now());
					}else {
						if(orderOrigin.getFinishTime()==null){
							log.info("完成过，未设置时间");
							setData.putAttribute(OrderHWANE16677Item.基本属性组_完成时间, LocalDateTime.now());
						}
					}
				}
			}
		}
	}


	private boolean isGood(Order order){
		return
				String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(order.getIsRefund()) &&
						String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(order.getIsCancel());
	}

	@Override
	public void fetchImprove(FuncGroupContext funcGroupContext, FGRecord fgRecord, FetchResultFactory fetchResultFactory) {
		Order order=fgRecord.getBean(Order.class);
		BigDecimal money=getMoney(fgRecord,String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsRetail()));

		try {
			WeighOther weighOther =PublicMethod.getWeigh(order.getOrder(),1);
			if(weighOther !=null){
				PublicMethod.setPmWeight(order,fetchResultFactory, weighOther,order.getActualWeight(),OrderHWANE16677Item.基本属性组_皮重,OrderHWANE16677Item.基本属性组_毛重);
			}
		}catch (Exception e){
			log.info("数据处理异常:{}",JSON.toJSON(order));
		}
		fetchResultFactory.getRecordOpsBuilder()
				.putAttribute(OrderHWANE16677Item.基本属性组_销售金额,money)
		;
	}
	private BigDecimal getMoney(FGRecord fgRecord,boolean isRetail){
		BigDecimal money=BigDecimal.ZERO;
		RecordQueryProConJunctionFactory jf = CarbonPanel
				.getRecordQueryFactory(CarbonBaseConstant.扣款记录_kkjl)
				.getProConJunctionFactory()
				.addEqual(DeductionHWANE16713Item.基本属性组_是否散户扣款,isRetail);
		jf.getRelationCriterionFactory(CarbonBaseConstant.订单记录_ddjl)
				.setInRelationTypes(CarbonRelationType.RR_扣款记录_订单名称_订单记录)
				.setInGuestCodes(fgRecord.getRecordCode());
		List<Deduction> list=jf.goBack().queryBeans(Deduction.class);
		if(list!=null){
			for (Deduction x:list){
				money=money.add(BigDecimal.valueOf(x.getDeductionAmount()));
			}
		}
		return money;
	}

	/**
	 * 初始化数据
	 */
	private void initialize(Order order,ImproveResultFactory improveResultFactory){
		log.info("QAQ初始化数据");
		ProRecordOpsBuilder setData=improveResultFactory.getCurrentProRecordOpsBuilder();
		order.setOrder(PublicMethod.getOrderCodeOR());
		setData.putAttribute(OrderHWANE16677Item.基本属性组_单号,order.getOrder());

		// 6/26新加入 配送车辆不生成取货码
		if(String.valueOf(CarbonEnumKeyValue.订单信息_收货类型_自提_zt).equals(order.getCollectType())){
			order.setPickupCode(PublicMethod.getPickupCode());
			setData.putAttribute(OrderHWANE16677Item.基本属性组_8位取货码,order.getPickupCode());
			setData.putAttribute(OrderHWANE16677Item.基本属性组_补充车号,true);
		}else {
			setData.putAttribute(OrderHWANE16677Item.基本属性组_补充车号,false);
		}

		if(StrUtil.isBlank(order.getOrderSource())){
			setData.putAttribute(OrderHWANE16677Item.基本属性组_订单来源,CarbonEnumKeyValue.订单来源_PC_);
		}
		if(StrUtil.isBlank(order.getState())){
			setData.putAttribute(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_已创建_ycj);
			order.setState(String.valueOf(CarbonEnumKeyValue.订单记录_状态_已创建_ycj));
		}
		if(StrUtil.isBlank(order.getInputType())){
			setData.putAttribute(OrderHWANE16677Item.基本属性组_录入类型,CarbonEnumKeyValue.订单记录_录入类型_系统下单_xtxd);
		}
		if(StrUtil.isBlank(order.getIsCancel())){
			setData.putAttribute(OrderHWANE16677Item.基本属性组_是否作废,CarbonEnumKeyValue.是否_否_f);
			order.setIsCancel(String.valueOf(CarbonEnumKeyValue.是否_否_f));
		}
		if(StrUtil.isBlank(order.getIsRefund())){
			setData.putAttribute(OrderHWANE16677Item.基本属性组_是否退货,CarbonEnumKeyValue.是否_否_f);
			order.setIsRefund(String.valueOf(CarbonEnumKeyValue.是否_否_f));
		}

		setData.putAttribute(OrderHWANE16677Item.基本属性组_是否同步,false);
		setData.putAttribute(OrderHWANE16677Item.基本属性组_是否已开票,false);
		setData.putAttribute(OrderHWANE16677Item.基本属性组_是否已打印小票,false);
	}

	/**
	 * 验证重量处理
	 */
	private boolean verifyWeightHandling(ProRecord proRecord,ImproveResultFactory improveResultFactory,double weightD,boolean isRetailData){
		BigDecimal weight=BigDecimal.valueOf(weightD);
		log.info("QAQ验证重量:{}t 是否散户数据:{}",weight,isRetailData);
		if(weight.compareTo(BigDecimal.ZERO)<0){
			PublicMethod.returnMessage(improveResultFactory, PublicReturn.ORDER_0);
			return false;
		}

		long retailData;
		if(isRetailData){
			retailData=CarbonRelationType.RR_订单记录_散户_客户管理;
		}else {
			retailData=CarbonRelationType.RR_订单记录_客户名称_客户管理;
		}
		BigDecimal sumWeight=BigDecimal.ZERO;
		BigDecimal haveWeight=BigDecimal.ZERO;

		//充值客户下的
		RecordQueryProConJunctionFactory jf = CarbonPanel
				.getRecordQueryFactory(CarbonBaseConstant.充值记录_czjl).getProConJunctionFactory()
				.addGreaterThan(RechargeHWANE16698Item.基本属性组_可用余额,0)
				.addEqual(RechargeHWANE16698Item.基本属性组_作废,false)
				;
		jf.getRelationCriterionFactory(CarbonBaseConstant.客户管理_khgl)
				.setInRelationTypes(CarbonRelationType.RR_充值记录_客户名称_客户管理)
				.setInGuestCodes(proRecord.getGuestRecordCode(retailData))
		;

		List<Recharge> list=jf.goBack().addSortItem(PublicMethod.createTimeStr(CarbonBaseConstant.充值记录_czjl), SortDirection.ASC)
				.queryBeans(Recharge.class,3);
		if(list!=null){
			Map<String, Map<BigDecimal, PublicEntity.RechargeUnit>> map=PublicMethod.integration(list);
			if(map.containsKey(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理))){
				for (Map.Entry<BigDecimal, PublicEntity.RechargeUnit> mapS : map.get(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理)).entrySet()) {
					PublicEntity.RechargeUnit recharge=mapS.getValue();
					BigDecimal sum=recharge.getAvailableBalanceSum();
					BigDecimal unit=mapS.getKey();
					BigDecimal availableWeight=PublicMethod.divideBigDecimal(sum,unit,PublicConstant.weightDecimals, RoundingMode.DOWN);
					sumWeight=sumWeight.add(availableWeight);
				}
			}

			RecordQueryProConJunctionFactory orderListS = CarbonPanel
					.getRecordQueryFactory(CarbonBaseConstant.订单记录_ddjl).getProConJunctionFactory()
					.addEqual(OrderHWANE16677Item.基本属性组_是否作废,CarbonEnumKeyValue.是否_否_f)
					.addEqual(OrderHWANE16677Item.基本属性组_是否退货,CarbonEnumKeyValue.是否_否_f)
					.addNotEqual(OrderHWANE16677Item.基本属性组_状态,PublicConstant.OrderStateOver)
					.addNotEqual(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk)
					;
			
//			RecordQueryProConJunctionFactory orderListS;
//			String state=proRecord.getString(OrderHWANE16677Item.基本属性组_状态);
//			if(state.equals(String.valueOf(CarbonEnumKeyValue.订单记录_状态_待过一磅_dgyb))){
//				orderListS = CarbonPanel
//						.getRecordQueryFactory(CarbonBaseConstant.订单记录_ddjl).getProConJunctionFactory()
//						.addEqual(OrderHWANE16677Item.基本属性组_是否作废,CarbonEnumKeyValue.是否_否_f)
//						.addEqual(OrderHWANE16677Item.基本属性组_是否退货,CarbonEnumKeyValue.是否_否_f)
//						.addNotEqual(OrderHWANE16677Item.基本属性组_状态,PublicConstant.OrderStateOver)
//						.addNotEqual(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk)
//						.addNotEqual(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_已创建_ycj)
//				;
//			}else {
//				if(state.equals(String.valueOf(CarbonEnumKeyValue.订单记录_状态_待装卸货_dzxh))){
//					orderListS = CarbonPanel
//							.getRecordQueryFactory(CarbonBaseConstant.订单记录_ddjl).getProConJunctionFactory()
//							.addEqual(OrderHWANE16677Item.基本属性组_是否作废,CarbonEnumKeyValue.是否_否_f)
//							.addEqual(OrderHWANE16677Item.基本属性组_是否退货,CarbonEnumKeyValue.是否_否_f)
//							.addNotEqual(OrderHWANE16677Item.基本属性组_状态,PublicConstant.OrderStateOver)
//							.addNotEqual(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk)
//							.addNotEqual(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_已创建_ycj)
//							.addNotEqual(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_待过一磅_dgyb)
//							;
//				}else {
//					if(state.equals(String.valueOf(CarbonEnumKeyValue.订单记录_状态_待过二磅_dgeb))){
//						orderListS = CarbonPanel
//								.getRecordQueryFactory(CarbonBaseConstant.订单记录_ddjl).getProConJunctionFactory()
//								.addEqual(OrderHWANE16677Item.基本属性组_是否作废,CarbonEnumKeyValue.是否_否_f)
//								.addEqual(OrderHWANE16677Item.基本属性组_是否退货,CarbonEnumKeyValue.是否_否_f)
//								.addNotEqual(OrderHWANE16677Item.基本属性组_状态,PublicConstant.OrderStateOver)
//								.addNotEqual(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk)
//								.addNotEqual(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_已创建_ycj)
//								.addNotEqual(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_待过一磅_dgyb)
//								.addNotEqual(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_待装卸货_dzxh)
//						;
//					}else {
//						orderListS = CarbonPanel
//								.getRecordQueryFactory(CarbonBaseConstant.订单记录_ddjl).getProConJunctionFactory()
//								.addEqual(OrderHWANE16677Item.基本属性组_是否作废,CarbonEnumKeyValue.是否_否_f)
//								.addEqual(OrderHWANE16677Item.基本属性组_是否退货,CarbonEnumKeyValue.是否_否_f)
//								.addNotEqual(OrderHWANE16677Item.基本属性组_状态,PublicConstant.OrderStateOver)
//								.addNotEqual(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk)
//						;
//					}
//				}
//			}

			orderListS.getRelationCriterionFactory(CarbonBaseConstant.客户管理_khgl)
					.setInRelationTypes(retailData)
					.setInGuestCodes(proRecord.getGuestRecordCode(retailData));
			orderListS
					.getRelationCriterionFactory(CarbonBaseConstant.产品管理_cpgl)
					.setInRelationTypes(CarbonRelationType.RR_订单记录_产品物料_产品管理)
					.setInGuestCodes(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理));
			List<Order> orderList =orderListS.goBack().queryBeans(Order.class);
			if(orderList!=null){
				for (Order order : orderList) {
					if(String.valueOf(CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk).equals(order.getState())){
						haveWeight=PublicMethod.addBigDecimal(haveWeight,order.getActualWeight());
					}else {
						haveWeight=PublicMethod.addBigDecimal(haveWeight,order.getWeight());
					}
				}
			}
			haveWeight=haveWeight.add(weight);
			if(!proRecord.isNew()){
				Order order=CarbonPanel
						.getRecordQueryFactory(CarbonBaseConstant.订单记录_ddjl).getProConJunctionFactory()
						.addEqual(CarbonBaseConstant.COLUMN_ABP0001,proRecord.getRecordCode()).goBack().queryABean(Order.class);

				if(String.valueOf(PublicConstant.OrderStateOver).equals(order.getState())
						|| String.valueOf(CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk).equals(order.getState())){
					haveWeight=PublicMethod.subtractBigDecimal(haveWeight,order.getActualWeight()!=null?order.getActualWeight():BigDecimal.ZERO);
				}else {
					haveWeight=PublicMethod.subtractBigDecimal(haveWeight,order.getWeight());
				}

			}
			int comTo=PublicMethod.compareToBigDecimal(sumWeight,haveWeight);
			log.info("QAQ吨数计算:总可用扣款重量{}t 未出场总需求重量{}t 当前需求重量{}t",sumWeight,haveWeight,weight);
			if(comTo<0){
				PublicMethod.returnMessage(improveResultFactory, isRetailData ? PublicReturn.ORDER_31 : PublicReturn.ORDER_3);
				return false;
			};
		}else {
			boolean isOk=true;
			if(String.valueOf(PublicConstant.OrderStateOver).equals(proRecord.getString(OrderHWANE16677Item.基本属性组_状态))
			||String.valueOf(CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk).equals(proRecord.getString(OrderHWANE16677Item.基本属性组_状态))
			){
				Order orderOld=PublicMethod.getInfo(CarbonBaseConstant.订单记录_ddjl, proRecord.getRecordCode(),Order.class);
				if(orderOld!=null&&
						(String.valueOf(PublicConstant.OrderStateOver).equals(orderOld.getState())||
								String.valueOf(CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk).equals(orderOld.getState())
								)){
					//已出场不返回
					isOk=false;
				}
			}
			if(isOk){
				PublicMethod.returnMessage(improveResultFactory,isRetailData?PublicReturn.ORDER_11:PublicReturn.ORDER_1);
				return false;
			}
		}
		return true;
	}

	private boolean verifyWeightHandlingNoAll(ProRecord proRecord,ImproveResultFactory improveResultFactory,double weightD,boolean isRetailData){
		BigDecimal weight=BigDecimal.valueOf(weightD);
		log.info("QAQ验证重量:{}t 是否散户数据:{}",weight,isRetailData);

		boolean isExist=false;
		//验证是否已扣款
		RecordQueryProConJunctionFactory deductionCount=CarbonPanel
				.getRecordQueryFactory(CarbonBaseConstant.扣款记录_kkjl).getProConJunctionFactory()
				.addEqual(DeductionHWANE16713Item.基本属性组_是否散户扣款,isRetailData);
		deductionCount.getRelationCriterionFactory(CarbonBaseConstant.订单记录_ddjl)
				.setInRelationTypes(CarbonRelationType.RR_扣款记录_订单名称_订单记录)
				.setInGuestCodes(proRecord.getRecordCode());
		AggregateVO aggregateVO = deductionCount.goBack().queryCount();
		if(aggregateVO != null && aggregateVO.getCount() >0){
			isExist=true;
		}
		if(isExist){
			return true;
		}


		if(weight.compareTo(BigDecimal.ZERO)<0){
			PublicMethod.returnMessage(improveResultFactory, PublicReturn.ORDER_0);
			return false;
		}

		long retailData;
		if(isRetailData){
			retailData=CarbonRelationType.RR_订单记录_散户_客户管理;
		}else {
			retailData=CarbonRelationType.RR_订单记录_客户名称_客户管理;
		}
		BigDecimal sumWeight=BigDecimal.ZERO;
		BigDecimal haveWeight=BigDecimal.ZERO;

		//充值客户下的
		RecordQueryProConJunctionFactory jf = CarbonPanel
				.getRecordQueryFactory(CarbonBaseConstant.充值记录_czjl).getProConJunctionFactory()
				.addGreaterThan(RechargeHWANE16698Item.基本属性组_可用余额,0)
				.addEqual(RechargeHWANE16698Item.基本属性组_作废,false)
				;
		jf.getRelationCriterionFactory(CarbonBaseConstant.客户管理_khgl)
				.setInRelationTypes(CarbonRelationType.RR_充值记录_客户名称_客户管理)
				.setInGuestCodes(proRecord.getGuestRecordCode(retailData))
		;

		List<Recharge> list=jf.goBack().addSortItem(PublicMethod.createTimeStr(CarbonBaseConstant.充值记录_czjl), SortDirection.ASC)
				.queryBeans(Recharge.class,3);
		if(list!=null){
			Map<String, Map<BigDecimal, PublicEntity.RechargeUnit>> map=PublicMethod.integration(list);
			if(map.containsKey(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理))){
				for (Map.Entry<BigDecimal, PublicEntity.RechargeUnit> mapS : map.get(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理)).entrySet()) {
					PublicEntity.RechargeUnit recharge=mapS.getValue();
					BigDecimal sum=recharge.getAvailableBalanceSum();
					BigDecimal unit=mapS.getKey();
					BigDecimal availableWeight=PublicMethod.divideBigDecimal(sum,unit,PublicConstant.weightDecimals, RoundingMode.DOWN);
					sumWeight=sumWeight.add(availableWeight);
				}
			}
			haveWeight=haveWeight.add(weight);
			int comTo=PublicMethod.compareToBigDecimal(sumWeight,haveWeight);
			log.info("QAQ吨数计算:总可用扣款重量{}t 未出场总需求重量{}t 当前需求重量{}t",sumWeight,haveWeight,weight);
			if(comTo<0){
				PublicMethod.returnMessage(improveResultFactory, isRetailData ? PublicReturn.ORDER_31 : PublicReturn.ORDER_3);
				return false;
			};
		}else {
			boolean isOk=true;
			if(String.valueOf(PublicConstant.OrderStateOver).equals(proRecord.getString(OrderHWANE16677Item.基本属性组_状态))
					||String.valueOf(CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk).equals(proRecord.getString(OrderHWANE16677Item.基本属性组_状态))
			){
				Order orderOld=PublicMethod.getInfo(CarbonBaseConstant.订单记录_ddjl, proRecord.getRecordCode(),Order.class);
				if(orderOld!=null&&
						(String.valueOf(PublicConstant.OrderStateOver).equals(orderOld.getState())||
								String.valueOf(CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk).equals(orderOld.getState())
						)){
					//已出场不返回
					isOk=false;
				}
			}
			if(isOk){
				PublicMethod.returnMessage(improveResultFactory,isRetailData?PublicReturn.ORDER_11:PublicReturn.ORDER_1);
				return false;
			}
		}
		return true;
	}

	private synchronized boolean deductionAll(ProRecord proRecord,ImproveResultFactory improveResultFactory,BigDecimal weight,boolean isRetailData,boolean isDeduction){
		if(isDeduction){
			log.info("扣款开始");
			return deduction(proRecord,improveResultFactory,weight,isRetailData);
		}else {
			log.info("扣款回退开始");
			deductionRefund(proRecord,improveResultFactory);
		}
		return true;
	}

	/**
	 * 扣款
	 */
//	@Deprecated
//	private boolean deduction(ProRecord proRecord,ImproveResultFactory improveResultFactory,BigDecimal weight,boolean isRetailData){
//		log.info("QAQ是否散户扣款:{} 实际重量:{}t",isRetailData,weight);
//		long retailData;
//		if(isRetailData){
//			retailData=CarbonRelationType.RR_订单记录_散户_客户管理;
//		}else {
//			retailData=CarbonRelationType.RR_订单记录_客户名称_客户管理;
//		}
//		boolean isExist=false;
//		//验证是否已扣款
//		RecordQueryProConJunctionFactory deductionCount=CarbonPanel
//				.getRecordQueryFactory(CarbonBaseConstant.扣款记录_kkjl).getProConJunctionFactory()
//				.addEqual(DeductionHWANE16713Item.基本属性组_是否散户扣款,isRetailData);
//		deductionCount.getRelationCriterionFactory(CarbonBaseConstant.订单记录_ddjl)
//				.setInRelationTypes(CarbonRelationType.RR_扣款记录_订单名称_订单记录)
//				.setInGuestCodes(proRecord.getRecordCode());
//		if(deductionCount.goBack().queryCount()>0){
//			isExist=true;
//		}
//		//充值客户下的
//		if(!isExist){
//			RecordQueryProConJunctionFactory jf = CarbonPanel
//					.getRecordQueryFactory(CarbonBaseConstant.充值记录_czjl).getProConJunctionFactory()
//					.addGreaterThan(RechargeHWANE16698Item.基本属性组_可用余额,0)
//					.addEqual(RechargeHWANE16698Item.基本属性组_作废,false)
//					;
//			jf.getRelationCriterionFactory(CarbonBaseConstant.客户管理_khgl)
//					.setInRelationTypes(CarbonRelationType.RR_充值记录_客户名称_客户管理)
//					.setInGuestCodes(proRecord.getGuestRecordCode(retailData));
//			List<Recharge> list=jf.goBack().addSortItem(PublicMethod.createTimeStr(CarbonBaseConstant.充值记录_czjl), SortDirection.ASC)
//					.queryBeans(Recharge.class,3);
//			if(list!=null){
//				BigDecimal newneedWMoney=weight;
//				for(Recharge x:list){
//					if(x!=null&&x.getUnivalence()!=null&&x.getUnivalence().getGoods()!=null){
//						if(x.getUnivalence().getGoods().getRecordCode().equals(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理))){
//							log.info("QAQ当前产品充值记录:{}", JSON.toJSON(x));
//							BigDecimal sum=BigDecimal.valueOf(x.getAvailableBalance());
//							BigDecimal unit=BigDecimal.valueOf(x.getUnivalence().getUnit());
//							BigDecimal availableWeight=PublicMethod.divideBigDecimal(sum,unit,PublicConstant.weightDecimals, RoundingMode.DOWN);
//							int comTo=PublicMethod.compareToBigDecimal(availableWeight,newneedWMoney);
//							log.info("QAQ扣除计算:可用余额{},合同单价{},余额是否够用{}",sum,unit,comTo);
//							BigDecimal shiJi2;
//							if(comTo>=0){
//								//当前合同刚好扣除 足够扣除
//								BigDecimal shiJi=PublicMethod.multiplyBigDecimal(newneedWMoney,unit,PublicConstant.leastCDF,null);
//								if(shiJi.compareTo(sum)>0){
//									shiJi=sum;
//								}
//
//								if(x.getCurrency().equals(String.valueOf(CarbonEnumKeyValue.币种_USD_))){
//									shiJi2=shiJi;
//								}else {
//									shiJi2=PublicMethod.multiplyBigDecimal(shiJi,x.getExchange(),PublicConstant.leastCDF,RoundingMode.DOWN);
//								}
//								saveDeduction(improveResultFactory,x.getRecordCode(),proRecord.getRecordCode(),shiJi,isRetailData,newneedWMoney,shiJi2);
//								updateRecharge(improveResultFactory,x.getRecordCode(),PublicMethod.addBigDecimal(x.getActualDeduction(),shiJi),PublicMethod.subtractBigDecimal(x.getAvailableBalance(),shiJi));
//								break;
//							}else {
//								//不够扣除
//								if(x.getCurrency().equals(String.valueOf(CarbonEnumKeyValue.币种_USD_))){
//									shiJi2=sum;
//								}else {
//									shiJi2=PublicMethod.multiplyBigDecimal(sum,x.getExchange(),PublicConstant.leastCDF,RoundingMode.DOWN);
//								}
//								saveDeduction(improveResultFactory,x.getRecordCode(),proRecord.getRecordCode(),sum,isRetailData,availableWeight,shiJi2);
//								updateRecharge(improveResultFactory,x.getRecordCode(),x.getRechargeAmount(),0);
//								newneedWMoney=PublicMethod.subtractBigDecimal(newneedWMoney,availableWeight);
//							}
//						}else {
//							log.info("QAQ非当前产品充值记录:{}", JSON.toJSON(x));
//						}
//					}
//				}
//			}else {
//				PublicMethod.returnMessage(improveResultFactory,isRetailData?PublicReturn.ORDER_11:PublicReturn.ORDER_1);
//				return false;
//			}
//		}else {
//			log.info("QAQ已扣款");
//		}
//		return true;
//	}

	private boolean deduction(ProRecord proRecord,ImproveResultFactory improveResultFactory,BigDecimal weight,boolean isRetailData){
		log.info("QAQ是否散户扣款:{} 实际重量:{}t",isRetailData,weight);
		long retailData;
		if(isRetailData){
			retailData=CarbonRelationType.RR_订单记录_散户_客户管理;
		}else {
			retailData=CarbonRelationType.RR_订单记录_客户名称_客户管理;
		}
		boolean isExist=false;
		//验证是否已扣款
		RecordQueryProConJunctionFactory deductionCount=CarbonPanel
				.getRecordQueryFactory(CarbonBaseConstant.扣款记录_kkjl).getProConJunctionFactory()
				.addEqual(DeductionHWANE16713Item.基本属性组_是否散户扣款,isRetailData);
		deductionCount.getRelationCriterionFactory(CarbonBaseConstant.订单记录_ddjl)
				.setInRelationTypes(CarbonRelationType.RR_扣款记录_订单名称_订单记录)
				.setInGuestCodes(proRecord.getRecordCode());
		AggregateVO aggregateVO = deductionCount.goBack().queryCount();
		if(aggregateVO!=null&&aggregateVO.getCount()>0){
			isExist=true;
		}
		//充值客户下的
		if(!isExist){
			RecordQueryProConJunctionFactory jf = CarbonPanel
					.getRecordQueryFactory(CarbonBaseConstant.充值记录_czjl).getProConJunctionFactory()
					.addGreaterThan(RechargeHWANE16698Item.基本属性组_可用余额,0)
					.addEqual(RechargeHWANE16698Item.基本属性组_作废,false)
					;
			jf.getRelationCriterionFactory(CarbonBaseConstant.客户管理_khgl)
					.setInRelationTypes(CarbonRelationType.RR_充值记录_客户名称_客户管理)
					.setInGuestCodes(proRecord.getGuestRecordCode(retailData));
			List<Recharge> list=jf.goBack().addSortItem(PublicMethod.createTimeStr(CarbonBaseConstant.充值记录_czjl), SortDirection.ASC)
					.queryBeans(Recharge.class,3);
			if(list!=null){
				Map<String, Map<BigDecimal, PublicEntity.RechargeUnit>> map=PublicMethod.integration(list);
				// 订单扣款重量
				BigDecimal orderWeight=weight;
				String goodId=proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理);
				if(map.containsKey(goodId)){
					// 获取产品的价格map
					Map<BigDecimal, PublicEntity.RechargeUnit> map2=map.get(goodId);
					for (Map.Entry<BigDecimal, PublicEntity.RechargeUnit> entry : map2.entrySet()) {
						BigDecimal unit=entry.getKey();
						PublicEntity.RechargeUnit recharge=entry.getValue();
						// 可扣的重量 = 可用余额 / 单价
						BigDecimal availableWeight=PublicMethod.divideBigDecimal(recharge.getAvailableBalanceSum(),unit,PublicConstant.weightDecimals, RoundingMode.DOWN);
						if(PublicMethod.compareToBigDecimal(availableWeight,orderWeight)>=0){
							//当前单价刚好扣除 足够扣除
							BigDecimal shiJi=PublicMethod.multiplyBigDecimal(orderWeight,unit,PublicConstant.leastUSD,null);
							if(shiJi.compareTo(recharge.getAvailableBalanceSum())>0){
								shiJi=recharge.getAvailableBalanceSum();
							}

							boolean isOk=false;
							for (Recharge rechargeOne : recharge.getList()) {
								BigDecimal availableBalance=new BigDecimal(String.valueOf(rechargeOne.getAvailableBalance()));//充值记录可用余额
								BigDecimal moneyJs;
								BigDecimal weightJs;
								BigDecimal moneyJsSj;
								if(shiJi.compareTo(availableBalance)>0){
									moneyJs=availableBalance;
									weightJs=PublicMethod.divideBigDecimal(moneyJs,unit,PublicConstant.weightDecimals, RoundingMode.DOWN);
									//充值记录不够扣 扣完
									shiJi=shiJi.subtract(moneyJs);
									orderWeight=orderWeight.subtract(weightJs);
								}else {
									//充值记录够扣
									moneyJs=shiJi;
									weightJs=orderWeight;
									isOk=true;
								}

								if(rechargeOne.getCurrency().equals(String.valueOf(CarbonEnumKeyValue.币种_USD_))){
									moneyJsSj=moneyJs;
								}else {
									moneyJsSj=PublicMethod.multiplyBigDecimal(moneyJs,rechargeOne.getExchange(),PublicConstant.leastCDF,RoundingMode.DOWN);
								}
								saveDeduction(improveResultFactory,rechargeOne.getRecordCode(),proRecord.getRecordCode(),moneyJs,isRetailData,weightJs,moneyJsSj,unit,new BigDecimal(String.valueOf(rechargeOne.getUnivalence().getTaxRate())));
								updateRecharge(improveResultFactory,rechargeOne.getRecordCode(),PublicMethod.addBigDecimal(rechargeOne.getActualDeduction(),moneyJs),PublicMethod.subtractBigDecimal(rechargeOne.getAvailableBalance(),moneyJs));
								if(isOk){
									break;
								}
							}
							break;
						}else {
							//不够扣 扣完
							orderWeight=orderWeight.subtract(availableWeight);
							BigDecimal shiJi=new BigDecimal(String.valueOf(recharge.getAvailableBalanceSum()));//充值记录可用余额
							for (int i = 0; i < recharge.getList().size(); i++) {
								Recharge rechargeOne=recharge.getList().get(i);
								BigDecimal moneyJs;
								BigDecimal weightJs;
								BigDecimal moneyJsSj;
								moneyJs=new BigDecimal(String.valueOf(rechargeOne.getAvailableBalance()));
								if(i>=recharge.getList().size()-1){
									weightJs=availableWeight;
								}else {
									weightJs=PublicMethod.divideBigDecimal(moneyJs,unit,PublicConstant.weightDecimals, RoundingMode.DOWN);
								}
								shiJi=shiJi.subtract(moneyJs);
								availableWeight=availableWeight.subtract(weightJs);
								if(rechargeOne.getCurrency().equals(String.valueOf(CarbonEnumKeyValue.币种_USD_))){
									moneyJsSj=moneyJs;
								}else {
									moneyJsSj=PublicMethod.multiplyBigDecimal(moneyJs,rechargeOne.getExchange(),PublicConstant.leastCDF,RoundingMode.DOWN);
								}
								saveDeduction(improveResultFactory,rechargeOne.getRecordCode(),proRecord.getRecordCode(),moneyJs,isRetailData,weightJs,moneyJsSj,unit,new BigDecimal(String.valueOf(rechargeOne.getUnivalence().getTaxRate())));
								updateRecharge(improveResultFactory,rechargeOne.getRecordCode(),PublicMethod.addBigDecimal(rechargeOne.getActualDeduction(),moneyJs),PublicMethod.subtractBigDecimal(rechargeOne.getAvailableBalance(),moneyJs));
							}
							orderWeight=orderWeight.subtract(availableWeight);
						}
					}
				}else {
					PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_34);
					return false;
				}
			}else {
				PublicMethod.returnMessage(improveResultFactory,isRetailData?PublicReturn.ORDER_11:PublicReturn.ORDER_1);
				return false;
			}
		}else {
			log.info("QAQ已扣款");
		}
		return true;
	}

	/**
	 * add扣款记录
	 * @param str1 充值记录id
	 * @param str2 订单记录id
	 * @param obj1 扣款金额
	 */
	private void saveDeduction(ImproveResultFactory improveResultFactory,String str1,String str2,Object obj1,Object obj2,BigDecimal big1,BigDecimal big2){
		log.info("插入扣款记录:订单id{},扣款金额{},是否散户扣款{},重量{},充值币种扣款{}",str2,obj1,obj2,big1,big2);
		improveResultFactory.getProRecordOpsBuilder(CarbonBaseConstant.扣款记录_kkjl)
				.addRelation(CarbonRelationType.RR_扣款记录_充值名称_充值记录,str1)
				.addRelation(CarbonRelationType.RR_扣款记录_订单名称_订单记录,str2)
				.putAttribute(DeductionHWANE16713Item.基本属性组_扣款金额,obj1)
				.putAttribute(DeductionHWANE16713Item.基本属性组_是否散户扣款,obj2)
				.putAttribute(DeductionHWANE16713Item.基本属性组_重量,big1)
				.putAttribute(DeductionHWANE16713Item.基本属性组_充值币种扣款,big2);
	}
	private void saveDeduction(ImproveResultFactory improveResultFactory,String str1,String str2,Object obj1,Object obj2,BigDecimal big1,BigDecimal big2,BigDecimal big3,BigDecimal big4){
		log.info("插入扣款记录:订单id{},扣款金额{},是否散户扣款{},重量{},充值币种扣款{}",str2,obj1,obj2,big1,big2);
		improveResultFactory.getProRecordOpsBuilder(CarbonBaseConstant.扣款记录_kkjl)
				.addRelation(CarbonRelationType.RR_扣款记录_充值名称_充值记录,str1)
				.addRelation(CarbonRelationType.RR_扣款记录_订单名称_订单记录,str2)
				.putAttribute(DeductionHWANE16713Item.基本属性组_扣款金额,obj1)
				.putAttribute(DeductionHWANE16713Item.基本属性组_是否散户扣款,obj2)
				.putAttribute(DeductionHWANE16713Item.基本属性组_重量,big1)
				.putAttribute(DeductionHWANE16713Item.基本属性组_单价,big3)
				.putAttribute(DeductionHWANE16713Item.基本属性组_税率,big4)
				.putAttribute(DeductionHWANE16713Item.基本属性组_充值币种扣款,big2);
	}

	/**
	 * 修改充值记录
	 * @param str1 充值记录id
	 * @param obj1 实际扣款
	 * @param obj2 可用余额
	 */
	private void updateRecharge(ImproveResultFactory improveResultFactory,String str1,Object obj1,Object obj2){
		log.info("修改充值记录:充值id{},实际扣款{},可用余额{}",str1,obj1,obj2);
		improveResultFactory.getProRecordOpsBuilder(CarbonBaseConstant.充值记录_czjl,str1)
				.putAttribute(RechargeHWANE16698Item.基本属性组_实际扣款,obj1)
				.putAttribute(RechargeHWANE16698Item.基本属性组_可用余额,obj2);
	}

	/**
	 * 计算运费
	 */
	private void calculateShipping(ProRecord proRecord,Order order,ImproveResultFactory improveResultFactory){
		try {
			if(
					(String.valueOf(PublicConstant.OrderStateOver).equals(order.getState())
					||String.valueOf(CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk).equals(order.getState()))
					&&String.valueOf(CarbonEnumKeyValue.订单信息_收货类型_配送_ps).equals(order.getCollectType())

			){
				boolean isSpecializedDelivery=true;
				String goodId=proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理);
				if(goodId==null){
					log.info("运费计算异常产品为空");
				}else {
					Goods good=CarbonPanel.getRecordDiscover(CarbonBaseConstant.产品管理_cpgl).getRecordBean(goodId,Goods.class,1);
					if(good==null){
						log.info("运费计算异常产品为空");
					}
					//是否配送水泥
					if(good!=null&&!StrUtil.isBlank(good.getCollectType())){
						if(String.valueOf(CarbonEnumKeyValue.订单信息_收货类型_配送_ps).equals(good.getCollectType())){
							log.info("不用送货计算运费，产品配置运费:{},固定运费为空赋值0",good.getFreight());
							isSpecializedDelivery=false;
							improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_运费,good.getFreight()==null?BigDecimal.ZERO:good.getFreight());
						}
					}
				}

				if(isSpecializedDelivery){
					log.info("送货点计算运费");

					PickupPoint pickupPoint=PublicMethod.getInfo(CarbonBaseConstant.提货点_thd,proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_提货点_提货点),PickupPoint.class);
					BigDecimal yunFei=BigDecimal.ZERO;
					if(pickupPoint!=null&&pickupPoint.getIsByCar()!=null){
						if(pickupPoint.getIsByCar()){
							yunFei=pickupPoint.getFreight();
						}else {
							BigDecimal util=pickupPoint.getTaxFare();
							if(order.getActualWeight()!=null&&util!=null){
								yunFei=PublicMethod.multiplyBigDecimal(order.getActualWeight(),util,PublicConstant.leastUSD,null);
							}
						}
						improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_运费,yunFei);
					}
				}
			}
		}catch (Exception e){
			log.info("异常{}",e.getMessage());
		}
	}

	/**
	 * IC卡逻辑 改为待过一磅
	 */
	private boolean ICCar(ProRecord proRecord,ImproveResultFactory improveResultFactory){
		log.info("IC卡逻辑");
		if(String.valueOf(PublicConstant.OrderStateOver).equals(proRecord.getString(OrderHWANE16677Item.基本属性组_状态))){
			return true;
		}

		boolean isOk=true;
		if(StrUtil.isNotBlank(proRecord.getString(OrderHWANE16677Item.基本属性组_IC卡号))){
			isOk =icCardHWANE16640BNB.verifyCardNumber(improveResultFactory,proRecord.getRecordCode(),proRecord.getString(OrderHWANE16677Item.基本属性组_IC卡号));
			if(isOk){
				if(proRecord.isNew()){
					improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_待过一磅_dgyb);
				}else {
					Order orderOld=CarbonPanel.getRecordDiscover(CarbonBaseConstant.订单记录_ddjl).getRecordBean(proRecord.getRecordCode(), Order.class);
					if(orderOld!=null){
						if(String.valueOf(CarbonEnumKeyValue.订单记录_状态_已创建_ycj).equals(orderOld.getState())){
							improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_待过一磅_dgyb);
						}
					}
				}
			}
		}
		return isOk;
	}
	/**
	 * 触发甲骨文销售订单删除
	 */
	private void returnTriggered(ProRecord proRecord){
		Order order=proRecord.getBean(Order.class);
		log.info("QAQ触发甲骨文销售订单删除 是否同步:{}",order.isSync());
		if(order.isSync()){
			OracleEntity.DeleteOrder info=new OracleEntity.DeleteOrder();
			info.setSalesNum(order.getOrder());
			info.setEhId(proRecord.getRecordCode());
			asyncImpl.deleteOrder(info);
		}
	}
	/**
	 * 触发扣款回退
	 */
	private void deductionRefund(ProRecord proRecord,ImproveResultFactory improveResultFactory){
		Order orderOld=CarbonPanel.getRecordDiscover(CarbonBaseConstant.订单记录_ddjl).getRecordBean(proRecord.getRecordCode(),Order.class);
		if (String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(orderOld.getIsCancel())
				&& String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(proRecord.getString(OrderHWANE16677Item.基本属性组_是否作废))) {
			// 作废不退款
			return;
		}
		if(String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(orderOld.getIsRefund())
				&&String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(orderOld.getIsCancel())){
			log.info("QAQ扣款回退开始:{}",JSON.toJSON(proRecord.getBean(Order.class)));
			RecordQueryProConJunctionFactory deductionCount=CarbonPanel
					.getRecordQueryFactory(CarbonBaseConstant.扣款记录_kkjl).getProConJunctionFactory();
			deductionCount.getRelationCriterionFactory(CarbonBaseConstant.订单记录_ddjl)
					.setInRelationTypes(CarbonRelationType.RR_扣款记录_订单名称_订单记录)
					.setInGuestCodes(proRecord.getRecordCode());
			List<Deduction> list=deductionCount.goBack().queryBeans(Deduction.class);

			if(list!=null){
				list.forEach(x->{
					ProRecord rroRecord=CarbonPanel.getRecordDiscover(CarbonBaseConstant.扣款记录_kkjl).getProRecord(x.getRecordCode());
					if(rroRecord!=null){
						Recharge recharge=CarbonPanel.getRecordDiscover(CarbonBaseConstant.充值记录_czjl).getRecordBean(rroRecord.getGuestRecordCode(CarbonRelationType.RR_扣款记录_充值名称_充值记录),Recharge.class);
						log.info("回退充值记录:{} 回退金额{}",JSON.toJSON(recharge),x.getDeductionAmount());
						IntegrationMsg msg=CarbonPanel.getFGRecordBuilder(CarbonBaseConstant.充值记录_czjl,recharge.getRecordCode())
								.putAttribute(RechargeHWANE16698Item.基本属性组_实际扣款,PublicMethod.subtractBigDecimal(recharge.getActualDeduction(),x.getDeductionAmount()))
								.putAttribute(RechargeHWANE16698Item.基本属性组_可用余额,PublicMethod.addBigDecimal(recharge.getAvailableBalance(),x.getDeductionAmount()))
								.integrate();
						if(msg.success()){
							log.info("回退成功");
						}else {
							log.info("回退失败");
							PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_14);
							return;
						}
					}
				});
			}
			log.info("QAQ扣款结束");
		}
	}

	@Override
	public boolean afterFusition(FuncGroupContext funcGroupContext, FuseContext fuseContext, String s) {
		Order order=CarbonPanel.getRecordDiscover(CarbonBaseConstant.订单记录_ddjl).getRecordBean(s,Order.class,2);
		//触发数据同步
		log.info("数据同步是否开始:{}",String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(order.getIsRetail()));
		//注释散户不实时同步
//		if(String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(order.getIsRetail())){
			FGRelationCorrelation relation=CarbonPanel.getRecordDiscover(CarbonBaseConstant.订单记录_ddjl).getRelationCorrelation(s);
			saveOrderNew(order,relation,true);
//		}

		//生成提货码
		if(!saveBillOfLading(order)){
		}

		//触发客户余额失效
		if(String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsRetail())){
			PublicMethod.dataPermanent.invalidate(PublicConstant.redisHeaderKhCq+order.getCustomer().getRecordCode());
			PublicMethod.dataPermanent.invalidate(PublicConstant.redisHeaderKhCq+order.getCustomerS().getRecordCode());
		}
		if(String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(order.getIsRetail())){
			PublicMethod.dataPermanent.invalidate(PublicConstant.redisHeaderKhCq+order.getCustomer().getRecordCode());
		}
		return true;
	}
	/**
	 * 已创建 待过一磅 有车号生成  散户 经销商配送单  提货码
	 * @param order
	 */
	public boolean saveBillOfLading(Order order){
		if(order!=null&&(order.getIsGenerate()==null || !order.getIsGenerate())){
			if(
					(
							order.getCar()!=null
									&&order.getIsSupplyCar()
//					&&(String.valueOf(CarbonEnumKeyValue.订单记录_状态_已创建_ycj).equals(order.getState())
//					||String.valueOf(CarbonEnumKeyValue.订单记录_状态_待过一磅_dgyb).equals(order.getState()))
					)
							&&
							(
									String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsRetail())
											||
											(String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(order.getIsRetail())
													&&String.valueOf(CarbonEnumKeyValue.订单信息_收货类型_配送_ps).equals(order.getCollectType()))
							)
			){
				log.info("打印信息提货单:{}",order.getOrder());
				Map<String,Object> data = new HashMap<>();
				if(String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsRetail())){
					data.put("data1",order.getCustomerS().getName());
				}
				if(String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(order.getIsRetail())){
					data.put("data1",order.getCustomer().getName());
				}

				data.put("data2",order.getGoods().getName());
				data.put("data31",order.getOrder());
				if(String.valueOf(CarbonEnumKeyValue.订单记录_类型_袋装_dz).equals(order.getType())){
					data.put("data32",order.getBags());
				}else {
					data.put("data32",order.getWeight());
				}
				data.put("data41",order.getCar().getCarNo());data.put("data42",order.getPickupCode());
				data.put("data5",order.getRemark());

				log.info("打印信息:{}", JSON.toJSON(data));

				BytesInfoVO file=null;
				if(String.valueOf(CarbonEnumKeyValue.订单信息_收货类型_配送_ps).equals(order.getCollectType())){
					FGRecord fGRecord=fileTemplateHWANE18473BNB.getInfo(CarbonEnumKeyValue.文件模板_销售提货单配送_xsthdps);
					if(fGRecord==null){
						log.info("打印失败无提货单模板");
						return false;
					}
					file=new BytesInfoVO("documents.pdf", PDFUtil.getBytesDeliveryCodeAnDa(fileTemplateHWANE18473BNB.getInfoFile(fGRecord),data));
				}else {
					FGRecord fGRecord=fileTemplateHWANE18473BNB.getInfo(CarbonEnumKeyValue.文件模板_销售提货单_xsthd);
					if(fGRecord==null){
						log.info("打印失败无提货单模板");
						return false;
					}
					file=new BytesInfoVO("documents.pdf", PDFUtil.getBytesDeliveryCode(fileTemplateHWANE18473BNB.getInfoFile(fGRecord),data));
				}

				FGRecordComplexusBuilder fGRecordComplexusBuilder=CarbonPanel.getFGRecordComplexusBuilder();
				fGRecordComplexusBuilder.getProRecordBuilder(CarbonBaseConstant.文件_wj)
						.addRelation(CarbonRelationType.RR_文件_属于订单记录提货点_订单记录,order.getRecordCode())
						.putAttribute(FileHWANE18503Item.基本属性组_文件,file);
				IntegrationMsg msg=CarbonPanel.getRecordIntegration(CarbonBaseConstant.文件_wj).integrate(fGRecordComplexusBuilder.getFGRecordComplexus());
				if(msg.success()){
					CarbonPanel.getFGRecordBuilder(CarbonBaseConstant.订单记录_ddjl,order.getRecordCode()).putAttribute(OrderHWANE16677Item.基本属性组_提货单,true).integrate();
				}else {
					log.info("提货码生成异常");
				}
			}
		}
		return true;
	}
	/**
	 * 整合发往甲骨文订单数据
	 */
	public boolean saveOrder(Order order,FGRelationCorrelation relation,boolean isSync){
		boolean isRetail=order.getIsRetail().equals(String.valueOf(CarbonEnumKeyValue.是否_是_s));
		if(String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsCancel())
				||String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsRefund())
				||order.isSync()
		){
			return false;
		}

		if(!order.getState().equals(String.valueOf(PublicConstant.OrderStateOver))){
			log.info("未完成暂不同步");
			return false;
		}

		if(order.getActualWeight()!=null){
			BigDecimal bigDecimal=new BigDecimal(String.valueOf(order.getActualWeight()));
			if(bigDecimal.compareTo(BigDecimal.ZERO)<=0){
				log.info("空车出场，暂不同步");
				return false;
			}
		}

		log.info("同步订单开始:");
		List<OracleEntity.Order> listAll=new ArrayList<>();

		//获取过磅信息时间
		WeighOther gbInfo= PublicMethod.getWeigh(order.getOrder(),2);;
		LocalDate date;
		if(gbInfo!=null){
			log.info("过磅信息信息:{}",JSON.toJSON(gbInfo));
			date=gbInfo.getPassTime().toLocalDate();
//			date=gbInfo.getDate(PublicMethod.createTimeStr(CarbonBaseConstant.过磅信息_gbxx)).toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
		}else {
			log.info("同步异常无过磅信息:");
			return false;
		}

		//客户信息获取
		Customer khInfo= CarbonPanel.getRecordQueryFactory(CarbonBaseConstant.客户管理_khgl).getProConJunctionFactory()
				.addEqual(CarbonBaseConstant.COLUMN_ABP0001,relation.getGuestRecordCode(CarbonRelationType.RR_订单记录_客户名称_客户管理))
				.goBack().queryABean(Customer.class);

		//关联几个扣款记录
		RecordQueryProConJunctionFactory jf = CarbonPanel
				.getRecordQueryFactory(CarbonBaseConstant.扣款记录_kkjl)
				.getProConJunctionFactory()
				.addEqual(DeductionHWANE16713Item.基本属性组_是否散户扣款,isRetail);
		jf.getRelationCriterionFactory(CarbonBaseConstant.订单记录_ddjl)
				.setInRelationTypes(CarbonRelationType.RR_扣款记录_订单名称_订单记录)
				.setInGuestCodes(order.getRecordCode());
		List<Deduction> list= jf.goBack().queryBeans(Deduction.class);

		//多个扣款记录用同一个产品信息
		Goods goodsInfo= CarbonPanel.getRecordQueryFactory(CarbonBaseConstant.产品管理_cpgl).getProConJunctionFactory()
				.addEqual(CarbonBaseConstant.COLUMN_ABP0001,relation.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理))
				.goBack().queryABean(Goods.class,2);

		if(list!=null&&!list.isEmpty()){
			for (Deduction deduction : list) {
				log.info("扣款记录信息:{}",JSON.toJSON(deduction));
				//扣款记录对于充值记录信息（非补贴)
				RecordQueryProConJunctionFactory cczl = CarbonPanel
						.getRecordQueryFactory(CarbonBaseConstant.充值记录_czjl)
						.getProConJunctionFactory()
						.addNotEqual(RechargeHWANE16698Item.基本属性组_充值类型,CarbonEnumKeyValue.充值记录_充值类型_补贴_bt)
						;
				cczl.getRelationCriterionFactory(CarbonBaseConstant.扣款记录_kkjl)
						.setInRelationTypes(CarbonRelationType.RR_充值记录_属于扣款记录_扣款记录)
						.setInGuestCodes(deduction.getRecordCode());
				FGRecord fGRecord1= cczl.goBack().queryARecord();
				if(fGRecord1!=null){
					//充值记录对应单价信息
					log.info("充值记录信息:{}",fGRecord1.toJson());
					RecordQueryProConJunctionFactory htdj = CarbonPanel
							.getRecordQueryFactory(CarbonBaseConstant.合同单价_htdj)
							.getProConJunctionFactory();
					htdj.getRelationCriterionFactory(CarbonBaseConstant.充值记录_czjl)
							.setInRelationTypes(CarbonRelationType.RR_合同单价_充值名称_充值记录)
							.setInGuestCodes(fGRecord1.getRecordCode());
					htdj.getRelationCriterionFactory(CarbonBaseConstant.产品管理_cpgl)
							.setInRelationTypes(CarbonRelationType.RR_合同单价_产品_产品管理)
							.setInGuestCodes(goodsInfo.getRecordCode());
					Univalence univalence= htdj.goBack().queryABean(Univalence.class);
					List<OracleEntity.Order.Line> lines=new ArrayList<>();
					OracleEntity.Order.Line line=new OracleEntity.Order.Line();
					if(univalence!=null){
						log.info("单价信息:{}",JSON.toJSON(univalence));
						OracleEntity.Order recharge=new OracleEntity.Order();
						recharge.setEhId(order.getRecordCode());
						recharge.setDocumentNo(order.getOrder());
						recharge.setDate(PublicMethod.removeHeaderZero(PublicMethod.getDateToString(date,PublicEnum.DateString.date2.getCode()),"/"));
						recharge.setEntityId(khInfo.getCustomerId());
						recharge.setEntityName(khInfo.getName());
						recharge.setDeliveryOption(order.getCollectType().equals(String.valueOf(CarbonEnumKeyValue.订单信息_收货类型_自提_zt))?"客户自提":"公司配送");
						if(recharge.getDeliveryOption().equals("公司配送")){
							log.info("公司配送");
							PickupPoint pickupPoint=CarbonPanel.getRecordDiscover((CarbonBaseConstant.提货点_thd)).getRecordBean(relation.getGuestRecordCode(CarbonRelationType.RR_订单记录_提货点_提货点),PickupPoint.class);
							String freight = "0";
							//TODO 运费待修改
							if(pickupPoint!=null&&pickupPoint.getIsByCar()!=null){
								//按吨计费
								if(pickupPoint.getIsByCar()){
									// 总运费%总吨
									freight=PublicMethod.divideBigDecimal(order.getFare(),order.getActualWeight(),PublicConstant.leastUSD,null).toPlainString();
								}else {
									freight=pickupPoint.getTaxFare().toPlainString();
								}
								recharge.setPickupLocation(pickupPoint.getRecordCode());
								recharge.setFreight(freight);
							}
							//赋值车辆
							FGRelationCorrelation clRelation=CarbonPanel.getRecordDiscover(CarbonBaseConstant.车辆管理_clgl)
									.getRelationCorrelation(relation.getGuestRecordCode(CarbonRelationType.RR_订单记录_车牌号_车辆管理));
							String clBg=clRelation.getGuestRecordCode(CarbonRelationType.RR_车辆管理_办公用车_办公用车);
							if(clBg!=null){
								OfficeVehicles officeVehicles=CarbonPanel.getRecordDiscover((CarbonBaseConstant.办公用车_bgyc))
										.getRecordBean(clBg,OfficeVehicles.class);
								if(officeVehicles!=null){
									recharge.setCar(officeVehicles.getNsId());
									recharge.setCarDepartment(officeVehicles.getNsIdBm());
								}
							}
							line.setAmount(String.valueOf(deduction.getDeductionAmount()-Double.valueOf(freight)));
						}else{
							line.setAmount(String.valueOf(deduction.getDeductionAmount()));
						}
						recharge.setLicenseNo("");
						recharge.setMemo(order.getRemark());

						recharge.setExchangeRate("");
						recharge.setCurrency(PublicEnum.Currency.getCode(fGRecord1.getLongValue(RechargeHWANE16698Item.基本属性组_币种)));


						line.setItemId(goodsInfo.getMaterialAll().getItemId());
						line.setItemNum(goodsInfo.getMaterialAll().getItemNum());
						line.setUnit(goodsInfo.getMaterialAll().getUnit());
						line.setQuantity(String.valueOf(deduction.getWeight()));
						BigDecimal taxRate=new BigDecimal("18.00");
						if(univalence.getTaxRate()!=null){
							taxRate=new BigDecimal(String.valueOf(univalence.getTaxRate()));
						}
						line.setTaxRate(taxRate.setScale(2, RoundingMode.HALF_UP).toPlainString()+"%");

						line.setItemTaxRate(String.valueOf(univalence.getUnit()));
						line.setItemRate(PublicMethod.divideBigDecimal(univalence.getUnit(),PublicMethod.addBigDecimal(1,taxRate.divide(BigDecimal.valueOf(100))),6,null).toPlainString());
						lines.add(line);
						recharge.setLines(lines);
						listAll.add(recharge);
					}else {
						log.info("同步异常无单价信息:");return false;
					}
				}else {
					log.info("同步异常无充值记录信息:");return false;
				}
			}

		}else {
			log.info("同步异常无扣款记录信息:");
			return false;
		}

		if(isSync){
			asyncImpl.saveOrder(listAll);
			return true;
		}else {
			return oracleController.saveOrderNoSync(listAll);
		}
	}
	public boolean saveOrderNew(Order order,FGRelationCorrelation relation,boolean isSync){
		boolean isRetail=order.getIsRetail().equals(String.valueOf(CarbonEnumKeyValue.是否_是_s));
		if(String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsCancel())
				|| String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsRefund())){
			return false;
		}
		if (!String.valueOf(CarbonEnumKeyValue.操作命令_发送记录_同步_fsjltb).equals(order.getCommand())
				&& order.isSync()) {
			// 不是手动同步，且已经同步，不继续同步
			log.info("已经同步，且不是手工操作，暂不同步");
			return false;
		}
		if(!order.getState().equals(String.valueOf(PublicConstant.OrderStateOver))){
			log.info("未完成暂不同步");
			return false;
		}
		if(order.getActualWeight()!=null){
			BigDecimal bigDecimal=new BigDecimal(String.valueOf(order.getActualWeight()));
			if(bigDecimal.compareTo(BigDecimal.ZERO)<=0){
				log.info("空车出场，暂不同步");
				return false;
			}
		}
		log.info("同步订单开始:");
		List<OracleEntity.Order> listAll=new ArrayList<>();
		//获取过磅信息时间
		WeighOther gbInfo= PublicMethod.getWeigh(order.getOrder(),2);;

		//客户信息获取
		Customer khInfo= CarbonPanel.getRecordQueryFactory(CarbonBaseConstant.客户管理_khgl).getProConJunctionFactory()
				.addEqual(CarbonBaseConstant.COLUMN_ABP0001,relation.getGuestRecordCode(CarbonRelationType.RR_订单记录_客户名称_客户管理))
				.goBack().queryABean(Customer.class);

		//关联几个扣款记录
		RecordQueryProConJunctionFactory jf = CarbonPanel
				.getRecordQueryFactory(CarbonBaseConstant.扣款记录_kkjl)
				.getProConJunctionFactory()
				.addEqual(DeductionHWANE16713Item.基本属性组_是否散户扣款,isRetail);
		jf.getRelationCriterionFactory(CarbonBaseConstant.订单记录_ddjl)
				.setInRelationTypes(CarbonRelationType.RR_扣款记录_订单名称_订单记录)
				.setInGuestCodes(order.getRecordCode());
		List<Deduction> list= jf.goBack().queryBeans(Deduction.class,2);
		// 根据唯一标识排序
		if (!CollectionUtils.isEmpty(list) && list.size() > 1) {
			list = list.stream().sorted(Comparator.comparing(Deduction::getRecordCode)).collect(Collectors.toList());
		}

		//多个扣款记录用同一个产品信息
		Goods goodsInfo= CarbonPanel.getRecordQueryFactory(CarbonBaseConstant.产品管理_cpgl).getProConJunctionFactory()
				.addEqual(CarbonBaseConstant.COLUMN_ABP0001,relation.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理))
				.goBack().queryABean(Goods.class,2);
		if (goodsInfo.getMaterialAll() == null) {
			log.info("产品未设置甲骨文物料，暂不同步");
			return false;
		}

		// fix：处理2024年12月12号之前扣款记录没有记录产品单价的问题
		if (list.get(0).getUnit() == null || list.get(0).getUnit().compareTo(BigDecimal.ZERO) <= 0) {
			Map<String, List<Univalence>> univalenceMap = new HashMap<>();
			list.forEach(deduction -> {
				Recharge recharge = deduction.getRecharge();
				List<Univalence> univalenceList = univalenceMap.computeIfAbsent(recharge.getRecordCode(), key -> {
					RecordQueryProConJunctionFactory query = PublicMethod.query(CarbonBaseConstant.合同单价_htdj);
					query.getRelationCriterionFactory(CarbonBaseConstant.充值记录_czjl)
							.setInRelationTypes(CarbonRelationType.RR_合同单价_充值名称_充值记录)
							.setInGuestCodes(recharge.getRecordCode());
					return query.goBack().queryBeans(Univalence.class, 2);
				});
				univalenceList.stream()
						.filter(univalence -> univalence.getGoods().getRecordCode().equals(goodsInfo.getRecordCode()))
						.findAny()
						.ifPresent(univalence -> {
							deduction.setUnit(new BigDecimal(univalence.getUnit().toString()));
							deduction.setTaxRate(new BigDecimal(univalence.getTaxRate().toString()));
						});
			});
		}

		if(list!=null&&!list.isEmpty()){
			OracleEntity.Order recharge=new OracleEntity.Order();
//			recharge.setExchangeRate(fGRecord1.getString(RechargeHWANE16698Item.基本属性组_计算汇率));
			recharge.setExchangeRate("1.00");//汇率
//			recharge.setCurrency(PublicEnum.Currency.getCode(fGRecord1.getLongValue(RechargeHWANE16698Item.基本属性组_币种)));
			recharge.setCurrency("USD");
			recharge.setDocumentNo(order.getOrder());
			recharge.setDate(PublicMethod.removeHeaderZero(PublicMethod.getDateToString(order.getFinishTime().toLocalDate(),PublicEnum.DateString.date2.getCode()),"/"));
			recharge.setEntityId(khInfo.getCustomerId());//NS客户主键
			recharge.setEntityName(khInfo.getName());//客户名称
			recharge.setEhId(order.getRecordCode());
			recharge.setDeliveryOption(order.getCollectType().equals(String.valueOf(CarbonEnumKeyValue.订单信息_收货类型_自提_zt))?"客户自提":"公司配送");
			recharge.setLicenseNo(order.getCar()==null?"":order.getCar().getCarNo());
			recharge.setMemo(order.getRemark());
			boolean isPs=recharge.getDeliveryOption().equals("公司配送");
			if(isPs){
				log.info("公司配送");
				PickupPoint pickupPoint=CarbonPanel.getRecordDiscover((CarbonBaseConstant.提货点_thd)).getRecordBean(relation.getGuestRecordCode(CarbonRelationType.RR_订单记录_提货点_提货点),PickupPoint.class);
				if (pickupPoint == null) {
					// 订单没有设置提货点，根据客户查询客户的提货点
					RecordQueryProConJunctionFactory query = PublicMethod.query(CarbonBaseConstant.提货点_thd);
					query.addEqual(PickupPointHWANE17409Item.基本属性组_是否删除, false);
					query.getRelationCriterionFactory(CarbonBaseConstant.客户管理_khgl)
							.setInRelationTypes(CarbonRelationType.RR_提货点_客户信息_客户管理)
							.setInGuestCodes(khInfo.getRecordCode());
					PickupPoint pickupPointByCustomer = query.goBack().queryABean(PickupPoint.class);
					if (pickupPointByCustomer != null) {
						setPickupPoint(order, relation, pickupPointByCustomer, recharge);
					} else {
						// 客户也没有设置提货点，根据产品名称获取固定的提货点
						pickupPoint = MockFreight.getPickPointByProduct(goodsInfo.getName());
						if (pickupPoint == null) {
							log.info("无提货点");
							return false;
						} else {
							setPickupPoint(order, relation, pickupPoint, recharge);
						}
					}
				} else {
					setPickupPoint(order, relation, pickupPoint, recharge);
				}
			}
			List<OracleEntity.Order.Line> lines=new ArrayList<>();


			Map<String, OracleEntity.MoneyWeight> map = new HashMap<>();
			for (Deduction deduction : list){
				String key = deduction.getUnit().toPlainString()+":"+deduction.getRecharge().getCurrency();
				if(map.containsKey(key)){
					map.get(key).setMoney(map.get(key).getMoney().add(new BigDecimal(String.valueOf(deduction.getDeductionAmount()))));
					map.get(key).setWeight(map.get(key).getWeight().add(new BigDecimal(String.valueOf(deduction.getWeight()))));
				}else {
					OracleEntity.MoneyWeight aa=new OracleEntity.MoneyWeight();
					aa.setMoney(new BigDecimal(String.valueOf(deduction.getDeductionAmount())));
					aa.setWeight(new BigDecimal(String.valueOf(deduction.getWeight())));
					aa.setTaxRate(deduction.getTaxRate());
					map.put(key,aa);
				}
			}
			for (Map.Entry<String, OracleEntity.MoneyWeight> entry : map.entrySet()) {
				String[] unitBz =entry.getKey().split(":");
				OracleEntity.MoneyWeight moneyWeight = entry.getValue();

				OracleEntity.Order.Line line=new OracleEntity.Order.Line();
				line.setItemId(goodsInfo.getMaterialAll().getItemId());
				line.setItemNum(goodsInfo.getMaterialAll().getItemNum());
				line.setUnit(goodsInfo.getMaterialAll().getUnit());
				line.setQuantity(moneyWeight.getWeight().toPlainString());

				BigDecimal dj=new BigDecimal(unitBz[0]);
				BigDecimal je=moneyWeight.getMoney();
				if(isPs&&recharge.getFreight()!=null){
					BigDecimal djBigDecimal=PublicMethod.subtractBigDecimal(unitBz[0],recharge.getFreight(),PublicConstant.leastUSD,null);
					dj=djBigDecimal.compareTo(BigDecimal.ZERO)<0?BigDecimal.ZERO:djBigDecimal;
					je=PublicMethod.multiplyBigDecimal(dj,moneyWeight.getWeight(),PublicConstant.leastUSD,null);
				}
				line.setTaxRate(moneyWeight.getTaxRate().setScale(2, RoundingMode.HALF_UP).toPlainString()+"%");
				line.setItemTaxRate(dj.toPlainString());
				line.setItemRate(dj.compareTo(BigDecimal.ZERO)<0?"0.00":PublicMethod.divideBigDecimal(dj,PublicMethod.addBigDecimal(1,moneyWeight.getTaxRate().divide(BigDecimal.valueOf(100))),6,null).toPlainString());
				line.setAmount(je.toPlainString());
				if(String.valueOf(CarbonEnumKeyValue.币种_USD_).equals(unitBz[1])){
					line.setMemo("USD");
				}else {
					line.setMemo("CDF");
				}
				lines.add(line);
			}

//			for (Deduction deduction : list) {
//				log.info("扣款记录信息:{}",JSON.toJSON(deduction));
//				OracleEntity.Order.Line line=new OracleEntity.Order.Line();
//
//				line.setItemId(goodsInfo.getMaterialAll().getItemId());
//				line.setItemNum(goodsInfo.getMaterialAll().getItemNum());
//				line.setUnit(goodsInfo.getMaterialAll().getUnit());
//				line.setQuantity(String.valueOf(deduction.getWeight()));
//
//				BigDecimal taxRate=deduction.getTaxRate()==null?BigDecimal.ZERO:deduction.getTaxRate();
//				line.setTaxRate(taxRate.setScale(2, RoundingMode.HALF_UP).toPlainString()+"%");
//
//				line.setItemTaxRate(deduction.getUnit().toPlainString());
//				line.setItemRate(PublicMethod.divideBigDecimal(deduction.getUnit(),PublicMethod.addBigDecimal(1,taxRate.divide(BigDecimal.valueOf(100))),6,null).toPlainString());
//				line.setAmount(String.valueOf(deduction.getDeductionAmount()));
//				line.setMemo("");
//
//				lines.add(line);
//			}
			recharge.setLines(lines);
			listAll.add(recharge);

		}else {
			log.info("同步异常无扣款记录信息:");
			return false;
		}

		if(isSync){
			asyncImpl.saveOrder(listAll);
			return true;
		}else {
			return oracleController.saveOrderNoSync(listAll);
		}
	}

	/**
	 * 设置提货点信息
	 */
	private void setPickupPoint(Order order, FGRelationCorrelation relation, PickupPoint pickupPoint, OracleEntity.Order recharge) {
		String freight = "0";
		//TODO 运费待修改
		//按吨计费
		if(pickupPoint.getIsByCar()){
			// 总运费%总吨
			freight=PublicMethod.divideBigDecimal(order.getFare(), order.getActualWeight(),PublicConstant.leastUSD,null).toPlainString();
		}else {
			freight= pickupPoint.getTaxFare().toPlainString();
		}
		recharge.setPickupLocation(pickupPoint.getRecordCode());
		recharge.setFreight(freight);
		//赋值车辆
		FGRelationCorrelation clRelation=CarbonPanel.getRecordDiscover(CarbonBaseConstant.车辆管理_clgl)
				.getRelationCorrelation(relation.getGuestRecordCode(CarbonRelationType.RR_订单记录_车牌号_车辆管理));
		String clBg=clRelation.getGuestRecordCode(CarbonRelationType.RR_车辆管理_办公用车_办公用车);
		if(clBg!=null){
			OfficeVehicles officeVehicles=CarbonPanel.getRecordDiscover((CarbonBaseConstant.办公用车_bgyc))
					.getRecordBean(clBg,OfficeVehicles.class);
			if(officeVehicles!=null){
				recharge.setCar(officeVehicles.getNsId());
				recharge.setCarDepartment(officeVehicles.getNsIdBm());
			}
		}
	}

	/**
	 * 手工补录
	 */
	public boolean inputHand(ProRecord proRecord,ImproveResultFactory improveResultFactory,Order order) {
		log.info("手工补录业务开始:{}",proRecord.toJson());
		ProRecordOpsBuilder setData=improveResultFactory.getCurrentProRecordOpsBuilder();

		if(PublicMethod.compareToBigDecimal(order.getWeight1(),order.getWeight2())>0){
			PublicMethod.returnMessage(improveResultFactory,PublicReturn.SUPPLY_ORDER_2);
			return false;
		}

		order.setActualWeight(PublicMethod.subtractBigDecimal(order.getWeight2(),order.getWeight1()).doubleValue());
		setData.putAttribute(OrderHWANE16677Item.基本属性组_实际重量,order.getActualWeight());

		boolean isRetail =String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsRetail());
		if(String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(order.getIsRefund())
				&&String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(order.getIsCancel())){
			log.info("验证实际重量");
			//新
			if(String.valueOf(PublicConstant.OrderStateOver).equals(order.getState())
					|| String.valueOf(CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk).equals(order.getState())){
				if(isRetail){
					if(!verifyWeightHandling(proRecord,improveResultFactory,order.getActualWeight(),true)){
						return false;
					}
				}else {
					if(!verifyWeightHandling(proRecord,improveResultFactory,order.getActualWeight(),false)){
						return false;
					}
				}
			}
			log.info("处理扣款");
			if(String.valueOf(PublicConstant.OrderStateOver).equals(order.getState())||String.valueOf(CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk).equals(order.getState())){
				if(isRetail){
					deductionAll(proRecord,improveResultFactory,BigDecimal.valueOf(order.getActualWeight()),true,true);
				}else {
					deductionAll(proRecord,improveResultFactory,BigDecimal.valueOf(order.getActualWeight()),false,true);
				}
			}
		}
		PublicMethod.saveWeighAll(improveResultFactory,order.getOrder(),BigDecimal.valueOf(order.getWeight1()),BigDecimal.ZERO,BigDecimal.ZERO,1,order.getWeight1Time(),null);
		PublicMethod.saveWeighAll(improveResultFactory,order.getOrder(),BigDecimal.ZERO,BigDecimal.valueOf(order.getWeight2()),BigDecimal.ZERO,2,order.getWeight2Time(),order.getWeight21());
		log.info("手工补录业务结束:");
		return true;
	}

	@Override
	public boolean beforeRemove(FuncGroupContext funcGroupContext, RemoveRecordInfo removeRecordInfo, DeleteMessageFactory defDeleteMessageFactory) {
		Collection<String> list=removeRecordInfo.getRecordCodes();
		log.info("删除业务开始:{}",list);
		boolean isOk=true;
		if(list!=null){
			for (String x : list) {
				FGRecord info=CarbonPanel.getRecordQueryFactory(CarbonBaseConstant.订单记录_ddjl).getProConJunctionFactory()
						.addEqual(CarbonBaseConstant.COLUMN_ABP0001,x)
						.addEqual(OrderHWANE16677Item.基本属性组_是否作废,CarbonEnumKeyValue.是否_否_f)
						.addEqual(OrderHWANE16677Item.基本属性组_是否退货,CarbonEnumKeyValue.是否_否_f)
						.addEqual(OrderHWANE16677Item.基本属性组_状态,PublicConstant.OrderStateOver)
						.goBack().queryARecord();
				if(info!=null){
					PublicMethod.returnMessage(defDeleteMessageFactory, PublicReturn.DELETE_1);
					isOk=false;
				}

				FGRecord info2=CarbonPanel.getRecordQueryFactory(CarbonBaseConstant.订单记录_ddjl).getProConJunctionFactory()
						.addEqual(CarbonBaseConstant.COLUMN_ABP0001,x)
						.addEqual(OrderHWANE16677Item.基本属性组_是否作废,CarbonEnumKeyValue.是否_否_f)
						.addEqual(OrderHWANE16677Item.基本属性组_是否退货,CarbonEnumKeyValue.是否_否_f)
						.addEqual(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk)
						.goBack().queryARecord();
				if(info2!=null){
					PublicMethod.returnMessage(defDeleteMessageFactory, PublicReturn.DELETE_1);
					isOk=false;
				}
			}
		}
		log.info("删除业务结束");
		return isOk;
	}

	@Override
	public boolean afterRemove(FuncGroupContext funcGroupContext, RemoveRecordInfo removeRecordInfo, DeleteMessageFactory defDeleteMessageFactory) {
		return true;
	}

	@Override
	public void preImprove(FuncGroupContext funcGroupContext, ProRecord proRecord, ProRecordOps proRecordOps, FGRecordComplexus fgRecordComplexus, FGOpsComplexus fgOpsComplexus, ImproveResultFactory improveResultFactory) {

	}

	@Override
	public void improve(FuncGroupContext funcGroupContext, ProRecord proRecord, FGRecordComplexus fgRecordComplexus, ImproveResultFactory improveResultFactory) {
		Order order=proRecord.getBean(Order.class);
		if(proRecord.isNew()){
			//修改类型传入方式
			String goodId=proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理);
			if(goodId==null){
				PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_20);
				return;
			}else {
				Goods good=CarbonPanel.getRecordDiscover(CarbonBaseConstant.产品管理_cpgl).getRecordBean(goodId,Goods.class,1);
				if(good==null){
					PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_22);
					return;
				}
				if(good.getType()==null){
					PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_23);
					return;
				}else {
					if(StrUtil.isBlank(order.getType())){
						improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_类型,good.getType());
						order.setType(good.getType());
					}
				}

				//是否配送水泥 为空时赋值
				if(StrUtil.isBlank(order.getCollectType())){
					if(!StrUtil.isBlank(good.getCollectType())){
						if(String.valueOf(CarbonEnumKeyValue.订单信息_收货类型_自提_zt).equals(good.getCollectType())){
							improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_收货类型,CarbonEnumKeyValue.订单信息_收货类型_自提_zt);
							order.setCollectType(String.valueOf(CarbonEnumKeyValue.订单信息_收货类型_自提_zt));
						}else {
//							if(good.getFreight()==null){
//								PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_24);
//								return;
//							}
							improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_收货类型,CarbonEnumKeyValue.订单信息_收货类型_配送_ps);
							order.setCollectType(String.valueOf(CarbonEnumKeyValue.订单信息_收货类型_配送_ps));
						}
					}else {
						PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_33);
						return;
					}
				}
			}

			//验证散户
			if(String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsRetail())){
				Customer customer=CarbonPanel.getRecordDiscover(CarbonBaseConstant.客户管理_khgl).getRecordBean(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_散户_客户管理),Customer.class,2);
				if(customer==null){
					PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_18);
					return;
				}
				if(customer.getCustomer()==null){
					PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_19);
					return;
				}
				PublicMethod.saveAddNow(improveResultFactory).addRelation(CarbonRelationType.RR_订单记录_客户名称_客户管理,customer.getCustomer().getRecordCode());
			}

			//经销商下单 二级经销商处理
			if(String.valueOf(CarbonEnumKeyValue.是否_否_f).equals(order.getIsRetail())){
				Customer customer=CarbonPanel.getRecordDiscover(CarbonBaseConstant.客户管理_khgl).getRecordBean(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_客户名称_客户管理),Customer.class,2);
				if(customer==null){
					PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_28);
					return;
				}
				if(String.valueOf(CarbonEnumKeyValue.客户管理_客户类型_二级经销商_ejjxs).equals(customer.getType())){
					if(customer.getCustomer()==null){
						PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_29);
						return;
					}
					PublicMethod.saveAddNow(improveResultFactory)
							.putAttribute(OrderHWANE16677Item.基本属性组_二级经销商,true)
							.addRelation(CarbonRelationType.RR_订单记录_客户名称_客户管理,customer.getCustomer().getRecordCode())
							.addRelation(CarbonRelationType.RR_订单记录_散户_客户管理,customer.getRecordCode());
				}
			}

			if(saveBatch(order,proRecord,improveResultFactory)){
				return;
			}

		}else {
			if(String.valueOf(CarbonEnumKeyValue.操作命令_手动还卡_sdhk).equals(order.getCommand())){
				OperationLogHWANE21797BNB.save(improveResultFactory,6,7,proRecord.getRecordCode());
				Order orderOld=CarbonPanel.getRecordDiscover(CarbonBaseConstant.订单记录_ddjl).getRecordBean(order.getRecordCode(),Order.class,1);
				if(orderOld!=null){
					if(String.valueOf(CarbonEnumKeyValue.订单记录_状态_待归还卡_dghk).equals(orderOld.getState())){
						improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_已出厂_ycc);
					}else {
						PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_27);
						return;
					}
				}
			}
			if(String.valueOf(CarbonEnumKeyValue.操作命令_手动装货_sdzh).equals(order.getCommand())){
				OperationLogHWANE21797BNB.save(improveResultFactory,6,6,proRecord.getRecordCode());
				Order orderOld=CarbonPanel.getRecordDiscover(CarbonBaseConstant.订单记录_ddjl).getRecordBean(order.getRecordCode(),Order.class,1);
				if(orderOld!=null){
					if(String.valueOf(CarbonEnumKeyValue.订单记录_状态_待装卸货_dzxh).equals(orderOld.getState())){
						improveResultFactory.getCurrentProRecordOpsBuilder().putAttribute(OrderHWANE16677Item.基本属性组_状态,CarbonEnumKeyValue.订单记录_状态_待过二磅_dgeb);
					}else {
						PublicMethod.returnMessage(improveResultFactory,PublicReturn.ORDER_26);
						return;
					}
				}
			}

			if(
					order.getExceptionPassOrNot()!=null&&order.getExceptionPassOrNot()||
							String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsCancel())||
							String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsRefund())
			){
				Order orderOld=CarbonPanel.getRecordDiscover(CarbonBaseConstant.订单记录_ddjl).getRecordBean(order.getRecordCode(),Order.class,1);
				if(orderOld!=null){
					if(order.getExceptionPassOrNot()!=null&&order.getExceptionPassOrNot()){
						if(!(orderOld.getExceptionPassOrNot()!=null&&orderOld.getExceptionPassOrNot())){
							OperationLogHWANE21797BNB.save(improveResultFactory,6,8,proRecord.getRecordCode());
						}
					}
					if(String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsCancel())){
						if(!(String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(orderOld.getIsCancel()))){
							OperationLogHWANE21797BNB.save(improveResultFactory,6,3,proRecord.getRecordCode());
						}
					}
					if(String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsRefund())){
						if(!(String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(orderOld.getIsRefund()))){
							OperationLogHWANE21797BNB.save(improveResultFactory,6,4,proRecord.getRecordCode());
						}
					}
				}
			}
		}
	}

	@Override
	public void postImprove(FuncGroupContext funcGroupContext, ProRecord proRecord, FGRecordComplexus fgRecordComplexus, ImproveResultFactory improveResultFactory) {
	}

	private boolean saveBatch(Order order,ProRecord proRecord,ImproveResultFactory improveResultFactory){
		if(order.getBatch()!=null&&order.getBatch()){
			boolean isHaveCar=String.valueOf(CarbonEnumKeyValue.订单信息_收货类型_自提_zt).equals(order.getCollectType());
			Integer saveNumber=order.getSaveNumber();
			Collection<String> listCarId=proRecord.getGuestRecordCodes(CarbonRelationType.RR_订单记录_多个车辆_车辆管理);
			Integer saveCar=listCarId==null?0:listCarId.size();
			if(isHaveCar){
//				if(order.getIsEqual()==null){
//					log.info("是否相同车为空");
//					return PublicMethod.returnMessageTrue(improveResultFactory,PublicReturn.ORDER_30);
//				}
//				if(order.getIsEqual()){
//					if(saveNumber==null||saveNumber<1){
//						log.info("相同车,创建单数为空或数据异常");
//						return PublicMethod.returnMessageTrue(improveResultFactory,PublicReturn.ORDER_30);
//					}
//				}else {
//					if(listCarId==null|| listCarId.isEmpty()){
//						log.info("不相同车,车号为空或数据异常");
//						return PublicMethod.returnMessageTrue(improveResultFactory,PublicReturn.ORDER_30);
//					}
//					saveNumber=listCarId.size();
//				}
				if(saveNumber==null||saveNumber<1||saveCar<1){
					log.info("车辆单数为空或车数为空");
					return PublicMethod.returnMessageTrue(improveResultFactory,PublicReturn.ORDER_30);
				}
			}else {
				if(saveNumber==null||saveNumber<1){
					log.info("相同车,创建单数为空或数据异常");
					return PublicMethod.returnMessageTrue(improveResultFactory,PublicReturn.ORDER_30);
				}
			}

			if(orderTypeInspection(proRecord,order,improveResultFactory)){
				return true;
			}

			double sumWeight=0.0;
			if(String.valueOf(CarbonEnumKeyValue.订单记录_类型_袋装_dz).equals(order.getType())){
				sumWeight=PublicMethod.multiplyBigDecimal(order.getBags(),configHWANE16627BNB.getConfigValueOneWeight(),PublicConstant.weightDecimals,null).multiply(BigDecimal.valueOf(saveNumber*saveCar)).doubleValue();
			}else {
				sumWeight=PublicMethod.multiplyBigDecimal(order.getWeight(),saveNumber*saveCar,PublicConstant.weightDecimals,null).doubleValue();
			}
			boolean isRetail =String.valueOf(CarbonEnumKeyValue.是否_是_s).equals(order.getIsRetail());
			if(isRetail){
				if(!verifyWeightHandling(proRecord,improveResultFactory,sumWeight,true)){
					return true;
				}
			}else {
				if(!verifyWeightHandling(proRecord,improveResultFactory,sumWeight,false)){
					return true;
				}
			}

			//从2开始
//			for (int i = 2; i <= saveNumber; i++) {
//				Order info=new Order();
//				info.setType(order.getType());
//				info.setWeight(order.getWeight());
//				info.setBags(order.getBags());
//				info.setSingleWeight(order.getSingleWeight());
//				info.setCollectType(order.getCollectType());
//				info.setIsRetail(order.getIsRetail());
//				info.setOrderSource(order.getOrderSource());
//				info.setInputType(order.getInputType());
//
//				info.setReceiptPrintedFlag(false);
//				info.setReceiptPrintedNumber(0);
//				info.setIsSupplyCar(false);
//				info.setIsGenerate(false);
//				info.setInvoicedFlag(false);
//				info.setLastLoadingVolume(0);
//				info.setActualBags(0);
//				info.setSupplementBags(0);
//				info.setActualWeight(0.0);
//
//				if(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_客户名称_客户管理)!=null){
//					info.setCustomerId(Long.valueOf(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_客户名称_客户管理)));
//				}
//				if(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_散户_客户管理)!=null){
//					info.setCustomerSId(Long.valueOf(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_散户_客户管理)));
//				}
//				if(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理)!=null){
//					info.setGoodsId(Long.valueOf(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理)));
//				}
//
//				if(isHaveCar){
//					if(order.getIsEqual()){
//						if(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_车牌号_车辆管理)!=null){
//							info.setCarId(Long.valueOf(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_车牌号_车辆管理)));
//						}
//						if(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_司机_司机管理)!=null){
//							info.setDriverId(Long.valueOf(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_司机_司机管理)));
//						}
//					}else {
//						info.setCarId(Long.valueOf(listCarId.toArray()[i-1].toString()));
//					}
//				}
//				improveResultFactory.putRecordBean(info);
//			}
//			if(isHaveCar){
//				if(!order.getIsEqual()){
//					improveResultFactory.getCurrentProRecordOpsBuilder().addRelation(CarbonRelationType.RR_订单记录_车牌号_车辆管理,listCarId.toArray()[0].toString());
//				}
//			}
			for (int i = 0; i < saveNumber; i++) {
				for (int j = 0; j < saveNumber; j++) {
					Order info=new Order();
					info.setType(order.getType());
					info.setWeight(order.getWeight());
					info.setBags(order.getBags());
					info.setSingleWeight(order.getSingleWeight());
					info.setCollectType(order.getCollectType());
					info.setIsRetail(order.getIsRetail());
					info.setOrderSource(order.getOrderSource());
					info.setInputType(order.getInputType());

					info.setReceiptPrintedFlag(false);
					info.setReceiptPrintedNumber(0);
					info.setIsSupplyCar(false);
					info.setIsGenerate(false);
					info.setInvoicedFlag(false);
					info.setLastLoadingVolume(0);
					info.setActualBags(0);
					info.setSupplementBags(0);
					info.setActualWeight(0.0);

					if(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_客户名称_客户管理)!=null){
						info.setCustomerId(Long.valueOf(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_客户名称_客户管理)));
					}
					if(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_散户_客户管理)!=null){
						info.setCustomerSId(Long.valueOf(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_散户_客户管理)));
					}
					if(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理)!=null){
						info.setGoodsId(Long.valueOf(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_产品物料_产品管理)));
					}
					if(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_司机_司机管理)!=null){
						info.setDriverId(Long.valueOf(proRecord.getGuestRecordCode(CarbonRelationType.RR_订单记录_司机_司机管理)));
					}
					if(isHaveCar){
						info.setCarId(Long.valueOf(listCarId.toArray()[i].toString()));
					}
					improveResultFactory.putRecordBean(info);
				}
			}
			improveResultFactory.removeRecord(new RecordRemoveDTO(CarbonBaseConstant.订单记录_ddjl,new RemoveDTO(null,order.getRecordCode(),null)) );
			return false;
		}
		return false;
	}
}
